import time from '@/util/getdateTime.js'; 
let config=uni.getStorageSync('config')
let _this = {
	//书架中是否已经存在
	async GetBookadd(NAME) {
		let BookList = uni.getStorageSync('bookshelf_list');
   
		if (BookList == "" || BookList == null || BookList == undefined) {
			return false;
		} else {
			let state = false;
			for (let i = 0; i < BookList.length; i++) {
				if (BookList[i].name == NAME) {
          var obj = BookList[i];
					BookList.splice(i, 1);
          BookList.unshift(obj)
					break;
				}
			}
			uni.setStorageSync('bookshelf_list', BookList);
		}
	},

	async hettimes(data) {
		let BookList = uni.getStorageSync('bookshelf_time');
		if(BookList == "" || BookList == null || BookList == undefined) {
      data.date=time.dateTimeStr('y-m-d h:i:s')
      data.is=(config.task_status == 1  && config.task_d_status == 1)?0:1;
			data.time=0
			uni.setStorageSync('bookshelf_time',[data]);
		} else {
		
			for (let i = 0; i < BookList.length; i++)
			{
				if(BookList[i].name == data.name) 
				{
          BookList[i].date=time.dateTimeStr('y-m-d')
					BookList[i].time=BookList[i].time+1;
          BookList[i].is=(config.task_status == 1  && config.task_d_status == 1)?0:1;
					uni.setStorageSync('bookshelf_time', BookList);
					break;
				}
			}
			if(!this.timeis(data)){
				BookList.push(data);  
				uni.setStorageSync('bookshelf_time', BookList);
			}
			
		}
	},
	async timeis(data){
	
			let BookList = uni.getStorageSync('bookshelf_time');
			if(BookList == "" || BookList == null || BookList == undefined) {
				return false;
			} else {
				for (let i = 0; i < BookList.length; i++)
				{
					if(BookList[i].name == data.name) {
						return true;
						break;
					}
				}
        data.time=time.dateTimeStr('y-m-d')
				data.time=0
        data.is=(config.task_status == 1  && config.task_d_status == 1)?0:1;
				BookList.push(data);
				uni.setStorageSync('bookshelf_time', BookList);
			}
		
	},
	//删除书籍
	async DelBookList(BookID) {
		let BookList = uni.getStorageSync('bookshelf_time');
		if(BookList == "" || BookList == null || BookList == undefined) {
			return false;
		} else {
			let newArr = [];
			for (let i = 0; i < BookList.length; i++)
			{
				if(BookID.includes(BookList[i].id)==false) {
					newArr.push(BookList[i]);
				}
			}
			uni.setStorageSync('bookshelf_time', newArr);
		}
	},
	//检测是否有缓存
	async GetBook(id, data, is) {
		let ids = id.replace('.', '_')
		let BookList = is ? undefined : uni.getStorageSync('books' + ids);
		if (BookList == "" || BookList == null || BookList == undefined) {
			if (data) {
				uni.setStorageSync('books' + ids, data)
				return data;
			} else {
				return false;
			}

		} else {
			return BookList
		}
	},
}

export default _this;
